/** 
 * File-Name:WeiboUserInfoRepository.java
 *
 * Created on 2011-11-11 下午09:40:30
 * 
 * @author: Neo (neolimeng@gmail.com)
 * Software Engineering Institute, Peking University, China
 * 
 * Copyright (c) 2009, Peking University
 * 
 *
 */
package site.service;

import java.sql.Connection;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import neoutil.CommonEntity;
import neoutil.CommonService;
import neoutil.DaoSupport;
import neoutil.Page;

import org.apache.commons.dbutils.DbUtils;
import org.apache.log4j.Logger;

import site.dao.ArticleDao;
import site.dao.WeiboUserInfoDao;
import site.entity.Article;
import site.entity.WeiboUserInfo;

/**
 * Description:
 * 
 * @author: Neo (neolimeng@gmail.com) Software Engineering Institute, Peking
 *          University, China
 * @version 1.0 2011-11-11 下午09:40:30
 */
public class WeiboUserInfoRepository extends CommonService {
	private static Logger log = Logger.getLogger(WeiboUserInfoRepository.class);
	private static String SQL_INSERT = "INSERT INTO weiboUserInfo(uuid, name, uid, verifier, entityStatus,groupType) VALUES(?, ?, ?, ?, ?, ?)";
	private static String SQL_UPDATE = "UPDATE weiboUserInfo SET uuid=?, name=?, uid=?, verifier=?, entityStatus=? WHERE id=?";
	private static String SQL_DELETE_BY_ID = "DELETE FROM weiboUserInfo WHERE id=?";
	private static String SQL_GET_BY_GROUPID = "SELECT * FROM weibouserinfo WHERE grouptype=? and entityStatus=1";

	public static void save(WeiboUserInfo userInfo) {
		Connection connection = null;

		try {
			connection = DaoSupport.getConnection();
			WeiboUserInfoDao userInfoDao = new WeiboUserInfoDao(connection);
			Object[] params = { userInfo.getUuid(), userInfo.getName(),
					userInfo.getUid(), userInfo.getVerifier(),
					CommonEntity.ENTITY_STATUS_NORMAL,
					userInfo.getGroupType()};
			userInfoDao.execute(SQL_INSERT, params);
		} catch (Exception e) {
			log.error("保存WeiboUserInfo对象出现异常！！");
			e.printStackTrace();
		} finally {
			closeConnection(connection);
		}
	}

	public static void delete(long id) {
		Connection connection = null;

		try {
			connection = DaoSupport.getConnection();
			WeiboUserInfoDao userInfoDao = new WeiboUserInfoDao(connection);
			userInfoDao.execute(SQL_DELETE_BY_ID, id);
		} catch (Exception e) {
			log.error("假删除WeiboUserInfo对象出现异常！！");
			e.printStackTrace();
		} finally {
			closeConnection(connection);
		}
	}

	public static Page<WeiboUserInfo> getAll(Page<WeiboUserInfo> page) {
		Connection connection = null;

		try {
			connection = DaoSupport.getConnection();
			WeiboUserInfoDao userInfoDao = new WeiboUserInfoDao(connection);
			return userInfoDao.getAll(page);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			closeConnection(connection);
		}

	}

	public static List<WeiboUserInfo> getAll() {
		Connection connection = null;

		try {
			connection = DaoSupport.getConnection();
			WeiboUserInfoDao userInfoDao = new WeiboUserInfoDao(connection);
			return userInfoDao.getAll();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			closeConnection(connection);
		}
	}
	
	//根据群组id查询用户
	public static List<WeiboUserInfo> getAllByGroupId(String groupId) {
		Connection connection = DaoSupport.getConnection();
		WeiboUserInfoDao weiboUserInfoDao = new WeiboUserInfoDao(connection);

		try {
			Object[] params = { groupId };
			List<WeiboUserInfo> users = weiboUserInfoDao.find(SQL_GET_BY_GROUPID, params);
			if (users == null || users.size() == 0) {
				log.error("查询不到可用的Group中的成员!");
				return null;
			} else {
				return users;
			}
		} catch (Exception e) {
			log.error("根据ID取得Article对象出现异常！！");
			e.printStackTrace();
			return null;
		} finally {
			try {
				DbUtils.close(connection);
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
	}

	/**
	 * Description:
	 * 
	 * @param args
	 *            void
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
